package gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import ordersList.OrderList;
import ordersList.OrdersManager;

public class screens_v01 {

	private JFrame frame;
	private JTextField textField_user;
	private JTextField textField_sessionID;
	private OrdersManager ordersManager;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					screens_v01 window = new screens_v01("user", Thread.currentThread().getId());
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public screens_v01(String userID, long sessionID) {
		initialize(userID, sessionID);
		ordersManager = new OrdersManager();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(String userID, long sessionID) {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.setBounds(80, 135, 117, 29);
		frame.getContentPane().add(btnConnect);
		
		JButton btnDisconnect = new JButton("Disconnect");
		btnDisconnect.setBounds(241, 135, 117, 29);
		frame.getContentPane().add(btnDisconnect);
		
		JButton btnMyOrders = new JButton("My Orders");
		btnMyOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel model = new TableModel(ordersManager, OrderList.Type.MY_ORDER_LIST);
				JTable table = new JTable(model);
				JScrollPane scroll = new JScrollPane(table);
				JFrame frame1 = new JFrame();
				frame1.add(scroll);
				frame1.setVisible(true);
				frame1.setSize(800, 600);
				frame1.setTitle("My Orders");
			}
		});
		btnMyOrders.setBounds(80, 176, 117, 29);
		frame.getContentPane().add(btnMyOrders);
		
		JButton btnBrowseOrders = new JButton("Browse Orders");
		btnBrowseOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel model = new TableModel(ordersManager,OrderList.Type.PUBLIC_ORDER_LIST);
				JTable table = new JTable(model);
				JScrollPane scroll = new JScrollPane(table);
				JFrame frame1 = new JFrame();
				frame1.add(scroll);
				frame1.setVisible(true);
				frame1.setSize(800, 600);
				frame1.setTitle("Browse Orders");
			}
		});
		btnBrowseOrders.setBounds(241, 176, 117, 29);
		frame.getContentPane().add(btnBrowseOrders);
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnExit.setBounds(164, 230, 117, 29);
		frame.getContentPane().add(btnExit);
		
		JLabel lblMicrotrader = new JLabel("MicroTrader v0.1");
		lblMicrotrader.setHorizontalAlignment(SwingConstants.CENTER);
		lblMicrotrader.setBounds(146, 16, 147, 16);
		frame.getContentPane().add(lblMicrotrader);
		
		JLabel lblUser = new JLabel("User:");
		lblUser.setBounds(97, 58, 61, 16);
		frame.getContentPane().add(lblUser);
		
		JLabel lblSessionId = new JLabel("Session ID: ");
		lblSessionId.setBounds(97, 86, 74, 16);
		frame.getContentPane().add(lblSessionId);
		
		textField_user = new JTextField();
		textField_user.setBounds(199, 52, 134, 28);
		frame.getContentPane().add(textField_user);
		textField_user.setColumns(10);
		textField_user.setText(userID);
		
		textField_sessionID = new JTextField();
		textField_sessionID.setBounds(199, 80, 134, 28);
		frame.getContentPane().add(textField_sessionID);
		textField_sessionID.setColumns(10);
		String session = String.valueOf(sessionID);
		textField_sessionID.setText(session);
		
		frame.setVisible(true);
	}
}
